import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

/* 
显示动画 触发分为四步骤
- 1. 声明动画控制器
- 2. 初始化动画控制器
- 3. 创建动画
- 4. 执行动画方法
  - repeat()
  - stop()
  - forward()
  - reset()
  - reverse()

动画分类
- RotationTransition  旋转动画


*/

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  // 1. 声明动画控制器
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    // 2. 初始化动画控制器
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 1),
      // lowerBound: 0.2, // 初识值
      // upperBound: 1,    // 更新值
    );
    // AnimationController(vsync: this, duration: Duration(seconds: 1))..repeat(); // 初始化执行动画
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.refresh),
          onPressed: () {
            //  4. 执行动画
            _controller.repeat();
          },
        ),
        appBar: AppBar(
          title: const Text('Title'),
        ),
        body: Center(
          child: Column(
            children: [
              // 3. 创建动画
              // 旋转动画
              RotationTransition(
                turns: _controller,
                child: FlutterLogo(
                  size: 60,
                ),
              ),
              // 透明度动画
              // FadeTransition(
              //   opacity: _controller,
              //   child: FlutterLogo(
              //     size: 120,
              //   ),
              // ),
              SizedBox(
                height: 400,
                child: Column(
                  children: [
                    ElevatedButton(
                        onPressed: () {
                          _controller.stop(); // 触发动画方法
                        },
                        child: Text('stop')),
                    ElevatedButton(
                        onPressed: () {
                          _controller.forward();
                        },
                        child: Text('forward')),
                    ElevatedButton(
                        onPressed: () {
                          _controller.reverse();
                        },
                        child: Text('reverse')),
                    ElevatedButton(
                        onPressed: () {
                          _controller.reset();
                        },
                        child: Text('reset')),
                    ElevatedButton(
                        onPressed: () {
                          _controller.repeat();
                        },
                        child: Text('repeat')),
                  ],
                ),
              ),
            ],
          ),
        ));
  }
}
